package com.baihui.taobao.transition;

import com.baihui.taobao.authorize.TaoBaoSessionInfo;
import com.baihui.taobao.vo.BUser;
import com.taobao.api.ApiException;
import com.taobao.api.domain.Chatpeer;
import com.taobao.api.request.WangwangEserviceChatpeersGetRequest;
import com.taobao.api.response.WangwangEserviceChatpeersGetResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kevin.
 * User: HSZM1106-0312
 * Date: 12-1-12
 * Time: 下午10:21
 */
public class WangWangCase extends Cases {
    public WangWangCase(TaoBaoSessionInfo info) {
        super(info);
    }

    /**
     * 得到最近七天的聊天记录
     */
    public List<Chatpeer> getUserListFromChat() throws Exception {
        WangwangEserviceChatpeersGetRequest req = new WangwangEserviceChatpeersGetRequest();
        req.setChatId("cntaobao" + getSessionInfo().getVisitor_nick());
        req.setStartDate("2012-01-01");
        req.setEndDate("2012-01-12");
        try {
            WangwangEserviceChatpeersGetResponse response = getTaoBaoClient().execute(req, getSessionInfo().getTopSession());
            return response.getChatpeers();
        } catch (ApiException e) {
            throw new Exception(e);
        }
    }


    public void importDataFromTaobao() throws Exception {
        List<Chatpeer> chatpeers = getUserListFromChat();
        if (chatpeers != null && chatpeers.size() > 0) {
            List<String> allChappter = new ArrayList<String>();
            for (Chatpeer chatpeer : chatpeers) {
                    allChappter.add(chatpeer.getUid().replace("cntaobao",""));
            }
            UserCase userCase = new UserCase(sessionInfo);
            userCase.importUserFromTaobao(allChappter, BUser.UNDEAL,null,getSessionInfo().getVisitor_id());
        }
    }

}
